package com.iplant.basic.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.iplant.base.po.*;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.po.excel.ExcelData;
import com.iplant.base.po.excel.ExcelSheetData;
import com.iplant.base.utils.general.Configuration;
import com.iplant.base.utils.general.LockHelper;
import com.iplant.base.utils.log.ErrorLogTool;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.base.utils.tasker.TaskTimerUtils;
import com.iplant.basic.service.BFCService;
import com.iplant.basic.service.basicenum.bfc.BFCMessageStatus;
import com.iplant.basic.service.basicenum.bfc.BFCMessageType;
import com.iplant.basic.service.basicenum.bfc.BaseTagType;
import com.iplant.basic.service.po.bfc.BFCHandleTaskMessage;
import com.iplant.basic.service.po.bfc.BFCHomePageGroup;
import com.iplant.basic.service.po.bfc.BFCHomePageModule;
import com.iplant.basic.service.po.bfc.BFCMessage;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.update.AppUpdateInfo;
import com.iplant.basic.service.po.update.CAPoHead;
import com.iplant.basic.serviceImpl.dao.TaskBaseDao;
import com.iplant.basic.serviceImpl.dao.bfc.*;
import com.iplant.basic.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

/**
 * @author xiuto
 */
@Slf4j
@Service
public class BFCServiceImpl implements BFCService {

    private static BFCService _instance = null;

    private static LockHelper mLockHelper = LockHelper.getLock(BFCServiceImpl.class);

    public static BFCService getInstance() {
        if (_instance == null) {
            synchronized (mLockHelper) {
                if (_instance == null) {
                    _instance = new BFCServiceImpl();
                    TaskTimerUtils.AddTask(() -> {

                        BFCHandleTaskMessage wBFCHandleTaskMessage = mBFCHandleTaskMessageResource.get();
                        if (wBFCHandleTaskMessage == null)
                            return;

                        ServiceResult<Integer> wServiceResult =
                                _instance.BFC_HandleTaskMessage(wBFCHandleTaskMessage.LoginUser,
                                        wBFCHandleTaskMessage.ResponsorID,
                                        wBFCHandleTaskMessage.TaskIDList, wBFCHandleTaskMessage.ModuleID,
                                        wBFCHandleTaskMessage.StepID,
                                        wBFCHandleTaskMessage.Status, wBFCHandleTaskMessage.Auto,
                                        wBFCHandleTaskMessage.CreateTime, Calendar.getInstance());

                        if (StringUtils.isNotEmpty(wServiceResult.FaultCode)) {
                            log.info("TaskTimerUtils BFC_HandleTaskMessage Error:{}", wServiceResult.FaultCode);
                        }
                    }, 2000, -1);

                }
            }
        }

        return _instance;
    }

    //region POService
    PoService<BFCHomePageModule> mBFCHomePageModuleService = ORMUtils.GetPoService(BFCHomePageModule.class);
    //endregion

    //region 历史接口

    @Override
    public ServiceResult<List<BFCHomePageGroup>> BFC_GetHomePageGroupList(
            BMSEmployee wLoginUser, String wProjectName, Integer wType, Integer wActive, String wName,
            Pagination wPagination) {
        ServiceResult<List<BFCHomePageGroup>> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            wResult.setResult(BFCHomePageGroupDAO.getInstance()
                    .BFC_SelectHomePageGroupAll(wLoginUser, wProjectName, wType, wActive, wName, wPagination,
                            wErrorCode));
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            log.error("BFC_GetHomePageGroupList:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_UpdateHomePageGroup(BMSEmployee wLoginUser, BFCHomePageGroup wBFCHomePageGroup) {
        ServiceResult<Integer> wResult = new ServiceResult<>();

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (wBFCHomePageGroup.Active != 3) {
                wResult.setResult(BFCHomePageGroupDAO.getInstance().UpdatePo(wLoginUser, wBFCHomePageGroup,
                        wErrorCode));
            } else {
                wResult.setResult(BFCHomePageGroupDAO.getInstance().DeletePO(wLoginUser, wBFCHomePageGroup, true,
                        wErrorCode));
            }
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            log.error("BFC_UpdateHomePageGroup:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BFCHomePageModule>> BFC_GetHomePageModuleList(
            BMSEmployee wLoginUser, String wProjectName, Integer wType, int wUserEnable, int wAdminEnable,
            int wEngineerEnable, int wSystemEnable, Integer wActive, String wName,
            int wGroupID, Pagination wPagination) {
        ServiceResult<List<BFCHomePageModule>> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            wResult.setResult(BFCHomePageModuleDAO.getInstance()
                    .BFC_SelectHomePageModuleAll(wLoginUser, wProjectName, wType, wUserEnable, wAdminEnable,
                            wEngineerEnable, wSystemEnable, wActive, wName, wGroupID,
                            wPagination,
                            wErrorCode));
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            log.error("BFC_GetHomePageModuleList:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<BFCHomePageModule> BFC_GetHomePageModuleByID(BMSEmployee wLoginUser, Integer wID) {
        // TODO Auto-generated method stub
        ServiceResult<BFCHomePageModule> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            wResult.setResult(BFCHomePageModuleDAO.getInstance().SelectPO(wLoginUser, wID, "", wErrorCode));
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("BFC_GetHomePageModuleByID:", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_UpdateHomePageModule(
            BMSEmployee wLoginUser, BFCHomePageModule wBFCHomePageModule) {
        ServiceResult<Integer> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (wBFCHomePageModule.Active != 3) {
                wResult.setResult(BFCHomePageModuleDAO.getInstance().UpdatePo(wLoginUser, wBFCHomePageModule, wErrorCode));
            } else {
                wResult.setResult(BFCHomePageModuleDAO.getInstance().DeletePO(wLoginUser, wBFCHomePageModule, true,
                        wErrorCode));
            }

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("BFC_UpdateHomePageModule:", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BFCMessage>> BFC_GetMessageList(
            BMSEmployee wLoginUser, Integer wResponsorID, Integer wType, List<Integer> wModuleID,
            List<Integer> wMessageIDList, List<Integer> wActive, Integer wSendStatus, Integer wShiftID,
            Calendar wStartTime, Calendar wEndTime, Integer wStepID, Pagination wPagination) {
        ServiceResult<List<BFCMessage>> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            wResult.Result = BFCMessageDAO.getInstance().BFC_GetMessageList(wLoginUser, wResponsorID, wType, wModuleID,
                    wMessageIDList, wActive, wSendStatus, wShiftID, wStartTime, wEndTime, wStepID, wPagination,
                    wErrorCode);

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            log.error("BFC_GetMessageList:", e);
            wResult.FaultCode += e.getMessage();
        }

        return wResult;
    }

    @Override

    public ServiceResult<Integer> BFC_GetMessageCount(
            BMSEmployee wLoginUser, Integer wResponsorID, Integer wType, List<Integer> wModuleID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            Map<Integer, Integer> wMessageCount = BFCMessageDAO.getInstance().BFC_GetMessageCount(wLoginUser,
                    wResponsorID, wModuleID, wErrorCode);

            if (wErrorCode.Result != 0) {
                wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
                return wResult;
            }
            if (wMessageCount == null || wMessageCount.size() == 0) return wResult;

            if (wType < 0) {
                wResult.Result = wMessageCount.values().stream().mapToInt(p -> p).sum();

            } else if (wMessageCount.containsKey(wType)) {
                wResult.Result = wMessageCount.get(wType);
            }

        } catch (Exception e) {
            log.error("BFC_GetMessageCount", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<BFCMessage>> BFC_GetUndoMessageList(
            BMSEmployee wLoginUser, Integer wResponsorID, List<Integer> wModuleID, Integer wMessageID, Integer wShiftID,
            Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<BFCMessage>> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.setResult(BFCMessageDAO.getInstance()
                    .BFC_GetUndoMessageList(wLoginUser, wResponsorID, wModuleID, wMessageID,
                            wShiftID, wStartTime, wEndTime, wPagination, wErrorCode));
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            log.error("BFCServiceImpl BFC_GetUndoMessageList ERROR:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_UpdateMessageList(BMSEmployee wLoginUser, List<BFCMessage> wBFCMessageList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wBFCMessageList != null && wBFCMessageList.size() > 0) {
                for (BFCMessage wBFCMessage : wBFCMessageList) {
                    BFCMessageDAO.getInstance().BFC_UpdateMessage(wLoginUser, wBFCMessage, wErrorCode);
                    wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
                }

                // 消息推送
                BFCServiceImpl.getInstance().BFC_WebSocketMessageSend(wLoginUser, wBFCMessageList);
            }
        } catch (Exception e) {
            log.error("BFC_UpdateMessageList:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_SendMessageList(
            BMSEmployee wLoginUser, List<BFCMessage> wBFCMessageList, int wSendStatus) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (wLoginUser == null) wLoginUser = BMSEmployee.SysAdmin;
            if (wBFCMessageList != null && wBFCMessageList.size() > 0) {

                BFCMessageDAO.getInstance().BFC_SendMessage(wLoginUser, wBFCMessageList, wSendStatus, wErrorCode);
                wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

            }

        } catch (Exception e) {
            log.error("BFCServiceImpl BFC_SendMessageList ERROR:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<Map<Integer, Integer>> BFC_GetUndoMessagCount(
            BMSEmployee wLoginUser, Integer wResponsorID, Integer wShiftID) {
        ServiceResult<Map<Integer, Integer>> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.setResult(BFCMessageDAO.getInstance()
                    .BFC_GetUndoMessageCount(wLoginUser, wResponsorID, wShiftID, wErrorCode));
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            log.error("BFCServiceImpl BFC_GetUndoMessagCount ERROR:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_ReceiveMessage(
            BMSEmployee wLoginUser, Integer wResponsorID, List<Integer> wMsgIDList, List<Integer> wStepID,
            Integer wModuleID, Calendar wMin, Calendar wMax) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (wMsgIDList != null && wMsgIDList.size() > 0) {
                BFCMessageDAO.getInstance().BFC_ReceiveMessage(wLoginUser, wResponsorID, wMsgIDList, wStepID,
                        wModuleID, wMin, wMax,
                        wErrorCode);
                wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
            }
        } catch (Exception e) {
            log.error("BFCServiceImpl BFC_ReceiveMessage ERROR:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_HandleMessageByIDList(
            BMSEmployee wLoginUser, List<Integer> wMsgIDList, Integer wStatus, Integer wSendStatus, Calendar wMin,
            Calendar wMax) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (wMsgIDList != null && wMsgIDList.size() > 0) {
                BFCMessageDAO.getInstance().BFC_HandleMessageByIDList(wLoginUser, wMsgIDList, wStatus, wSendStatus,
                        wMin, wMax,
                        wErrorCode);
                wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
            }
        } catch (Exception e) {
            log.error("BFCServiceImpl BFC_ReceiveMessage ERROR:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_HandleMessage(
            BMSEmployee wLoginUser, Integer wResponsorID, List<Integer> wMsgIDList, List<Integer> wStepID,
            Integer wModuleID, Integer wType, Integer wStatus, Calendar wMin, Calendar wMax) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (wMsgIDList != null && wMsgIDList.size() > 0) {
                BFCMessageDAO.getInstance().BFC_HandleMessage(wLoginUser, wResponsorID, wMsgIDList, wStepID, wModuleID,
                        wType, wStatus, wMin, wMax, wErrorCode);
                wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
            }
        } catch (Exception e) {
            log.error("BFCServiceImpl BFC_HandleMessage ERROR:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_ForwardMessage(
            BMSEmployee wLoginUser, Integer wResponsorID, List<Integer> wForwarderList, Integer wModuleID,
            Integer wMessageID, Integer wStepID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wModuleID <= 0 || wMessageID <= 0 || wStepID <= 0 || (wResponsorID <= 0 && wResponsorID != -100) ||
                    wForwarderList == null || wForwarderList.size() == 0) {
                return wResult;
            }

            BFCMessageDAO.getInstance().BFC_ForwardMessage(wLoginUser, wResponsorID, wForwarderList, wModuleID,
                    wMessageID, wStepID, wErrorCode);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            log.error("BFCServiceImpl BFC_HandleMessage ERROR:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_HandleTaskMessage(
            BMSEmployee wLoginUser, Integer wResponsorID, List<Integer> wTaskIDList, Integer wModuleID, Integer wStepID,
            Integer wStatus, Integer wAuto, Calendar wMin, Calendar wMax) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (wTaskIDList != null && wTaskIDList.size() > 0) {
                BFCMessageDAO.getInstance().BFC_HandleTaskMessage(wLoginUser, wResponsorID, wTaskIDList, wModuleID,
                        wStepID, wStatus, wAuto, wMin, wMax, wErrorCode);
                wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
            }
        } catch (Exception e) {
            log.error("BFCServiceImpl BFC_HandleTaskMessage ERROR:", e);
            wResult.FaultCode += e.getMessage();
        }
        return wResult;
    }

    /**
     * 查询已办消息列表
     */
    @Override
    public ServiceResult<List<BFCMessage>> BFC_GetMessageDoneList(
            BMSEmployee wLoginUser, Integer wResponsorID, List<Integer> wModuleID, Calendar wStartTime,
            Calendar wEndTime, Pagination wPagination) {
        ServiceResult<List<BFCMessage>> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            List<BFCMessage> wBFCMessageList = new ArrayList<>();

            wBFCMessageList = BFCMessageDAO.getInstance().BFC_GetMessageList(wLoginUser, wResponsorID,
                    BFCMessageType.Task.getValue(), wModuleID, -1, -1,
                    StringUtils.parseListArgs(BFCMessageStatus.Finished.getValue()), -1, -1, wStartTime, wEndTime,
                    wPagination, wErrorCode);
            wResult.setResult(wBFCMessageList);
            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
        } catch (Exception e) {
            // TODO: handle exception
            wResult.FaultCode += e.getMessage();
        }

        return wResult;
    }

    /**
     * 查询通知列表
     */

    @Override
    public ServiceResult<List<BFCMessage>> BFC_GetNoticeList(
            BMSEmployee wLoginUser, Integer wResponsorID, List<Integer> wModuleID, Integer wStatus, Integer wUseTime,
            Calendar wStartTime, Calendar wEndTime, OutResult<Integer> wCount, Pagination wPagination) {
        ServiceResult<List<BFCMessage>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        wCount.set(0);

        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            Map<Integer, Integer> wMessageCount = BFCMessageDAO.getInstance().BFC_GetMessageCount(wLoginUser,
                    wResponsorID, wModuleID, wErrorCode);

            if (wErrorCode.Result != 0) {
                wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();
                return wResult;
            }
            if (wMessageCount.containsKey(BFCMessageType.Notify.getValue())) {
                wCount.Result = wMessageCount.get(BFCMessageType.Notify.getValue());
            }

            if (wStatus != BFCMessageStatus.Read.getValue()) {
                if (wUseTime > 0) {
                    wResult.Result = BFCMessageDAO.getInstance().BFC_GetMessageList(wLoginUser, wResponsorID,
                            BFCMessageType.Notify.getValue(), wModuleID, -1, -1,
                            StringUtils.parseListArgs(BFCMessageStatus.Sent.getValue(),
                                    BFCMessageStatus.Default.getValue(), BFCMessageStatus.Read.getValue()), -1, -1,
                            wStartTime, wEndTime, wPagination, wErrorCode);
                } else {
                    wResult.Result = BFCMessageDAO.getInstance().BFC_GetMessageList(wLoginUser, wResponsorID,
                            BFCMessageType.Notify.getValue(), wModuleID, -1,
                            StringUtils.parseListArgs(BFCMessageStatus.Sent.getValue(),
                                    BFCMessageStatus.Default.getValue(), BFCMessageStatus.Read.getValue()), -1,
                            wPagination, wErrorCode);

                }

            } else {
                wResult.Result = BFCMessageDAO.getInstance().BFC_GetMessageList(wLoginUser, wResponsorID,
                        BFCMessageType.Notify.getValue(), wModuleID, -1, -1,
                        StringUtils.parseListArgs(BFCMessageStatus.Read.getValue()), -1, -1, wStartTime, wEndTime,
                        wPagination, wErrorCode);
            }

            wResult.FaultCode += ExceptionTypes.getEnumType(wErrorCode.get()).getLabel();

        } catch (Exception e) {
            // TODO: handle exception
            wResult.FaultCode += e.getMessage();
            log.error("BFCServiceImpl BFC_GetWaitNoticeList Error:", e);
        }

        return wResult;
    }

    @Override
    public ServiceResult<List<ErrorLogItem>> BFC_QueryLogFileList(BMSEmployee wLoginUser) {
        ServiceResult<List<ErrorLogItem>> wResult = new ServiceResult<>();
        try {
            wResult.Result = ErrorLogTool.getInstance().SelectList(wLoginUser);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<String>> BFC_SelectLogFile(BMSEmployee wLoginUser, String wFilePath) {
        ServiceResult<List<String>> wResult = new ServiceResult<>();
        try {
            wResult.Result = ErrorLogTool.getInstance().SelectFile(wLoginUser, wFilePath);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<Boolean> BFC_DeleteLogFile(BMSEmployee wLoginUser, String wFilePath) {
        ServiceResult<Boolean> wResult = new ServiceResult<>();
        try {
            wResult.Result = ErrorLogTool.getInstance().DeleteFile(wLoginUser, wFilePath);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<String> BFC_DeleteLogFileList(BMSEmployee wLoginUser, List<String> wFilePathList) {
        ServiceResult<String> wResult = new ServiceResult<>();
        try {
            wResult.Result = ErrorLogTool.getInstance().DeleteFileList(wLoginUser, wFilePathList);
        } catch (Exception ex) {
            wResult.FaultCode += ex.getMessage();
            log.error(ex.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<? extends BasePO>> BFC_GetTaskList(
            BMSEmployee wLoginUser, int wTag, String wIndexName, int wResponsorID, List<Integer> wEventIDs,
            Calendar wStartTime, Calendar wEndTime, Pagination wPagination) {

        ServiceResult<List<? extends BasePO>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (StringUtils.isEmpty(wIndexName) || wEventIDs == null || wEventIDs.size() == 0) {
                wResult.FaultCode = RetCode.SERVER_RST_ERROR_OUT;
                return wResult;
            }
            if (wResponsorID <= 0) {
                wResponsorID = wLoginUser.getID();
            }
            if (wResponsorID <= 0) {
                return wResult;
            }
            TaskBaseDao<?> wTaskBaseDao = TaskBaseDao.GetInstance(wIndexName);
            if (wTaskBaseDao == null) {
                return wResult;
            }

            Map<String, Object> wParams = new HashMap<String, Object>();

            switch (BaseTagType.getEnumType(wTag)) {

                case ToDo:
                    wResult.Result = wTaskBaseDao.BFC_GetUndoTaskList(
                            wLoginUser, wResponsorID, wEventIDs, wParams, wPagination, wErrorCode);

                    break;
                case Done:
                    wResult.Result = wTaskBaseDao.BFC_GetDoneTaskList(
                            wLoginUser, wResponsorID, wEventIDs, wStartTime, wEndTime, wParams, wPagination,
                            wErrorCode);
                    break;
                case Launch:
                    wResult.Result = wTaskBaseDao.BFC_GetSendTaskList(
                            wLoginUser, wResponsorID, wEventIDs, wStartTime, wEndTime, wParams, wPagination,
                            wErrorCode);
                    break;
                case Default:
                default:
                    break;
            }


            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error("BFC_GetTaskList", e);
        }
        return wResult;

    }


    @Override
    public ServiceResult<BasePO> BFC_GetTaskInfo(
            BMSEmployee wLoginUser, String wIndexName, int wID, String wCode) {

        ServiceResult<BasePO> wResult = new ServiceResult<>();
        wResult.Result = new BasePO();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (StringUtils.isEmpty(wIndexName)) {
                wResult.FaultCode = RetCode.SERVER_RST_ERROR_OUT;
                return wResult;
            }

            TaskBaseDao<?> wTaskBaseDao = TaskBaseDao.GetInstance(wIndexName);
            if (wTaskBaseDao == null) {
                return wResult;
            }

            wResult.Result = wTaskBaseDao.BFC_GetTaskInfo(wLoginUser, wID, wCode, wErrorCode);


            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error("BFC_GetTaskList", e);
        }
        return wResult;

    }

    @Override
    public ServiceResult<List<? extends BasePO>> BFC_UpdateTaskList(
            BMSEmployee wLoginUser, String wIndexName, List<Map> wTList, Integer wModuleID, Integer wStepID,
            int wStatus, Calendar wMin, Calendar wMax) {

        ServiceResult<List<? extends BasePO>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (StringUtils.isEmpty(wIndexName)) {
                wResult.FaultCode = RetCode.SERVER_RST_ERROR_OUT;
                return wResult;
            }

            TaskBaseDao<?> wTaskBaseDao = TaskBaseDao.GetInstance(wIndexName);
            if (wTaskBaseDao == null) {
                return wResult;
            }

            wResult.Result = wTaskBaseDao.BFC_UpdateTaskList(wLoginUser, wTList, wModuleID, wStepID, wStatus, wMin,
                    wMax,
                    wErrorCode);


            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error("BFC_GetTaskList", e);
        }
        return wResult;

    }

    //endregion

    //region 错误日志

    @Override
    public ServiceResult<Integer> ELG_EmptyFile(BMSEmployee wLoginUser, String wFilePath) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            FileWriter writer = new FileWriter(wFilePath);
            writer.write("");
            writer.close();

            log.info(StringUtils.Format("{0}：文件内容已清空", wFilePath));

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error("ELG_EmptyFile", e);
        }
        return wResult;
    }

    //endregion

    //region app更新

    @Override
    public ServiceResult<AppUpdateInfo> APP_QueryUpdateInfo(BMSEmployee wLoginUser) {
        ServiceResult<AppUpdateInfo> wResult = new ServiceResult<>();
        wResult.Result = new AppUpdateInfo();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.Result.platform = BFCSystemConfigDAO.getInstance().BFC_GetValueByName(wLoginUser, -1,
                    "update_platform", wErrorCode);
            wResult.Result.updateContent = BFCSystemConfigDAO.getInstance().BFC_GetValueByName(wLoginUser, -1,
                    "update_updateContent", wErrorCode);
            wResult.Result.downUrl = BFCSystemConfigDAO.getInstance().BFC_GetValueByName(wLoginUser, -1,
                    "update_downUrl", wErrorCode);
            wResult.Result.version = BFCSystemConfigDAO.getInstance().BFC_GetValueByName(wLoginUser, -1,
                    "update_version", wErrorCode);
            wResult.Result.force = BFCSystemConfigDAO.getInstance().BFC_GetValueByName(wLoginUser, -1, "update_force"
                    , wErrorCode).equals("1");
            wResult.Result.mainColor = BFCSystemConfigDAO.getInstance().BFC_GetValueByName(wLoginUser, -1,
                    "update_mainColor", wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<String> BFC_ImortPo(BMSEmployee wLoginUser, ExcelData wExcelData) {
        ServiceResult<String> wResult = new ServiceResult<>();
        wResult.Result = "";
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wExcelData == null || wExcelData.sheetData == null || wExcelData.sheetData.size() <= 0) {
                wResult.FaultCode += "导入失败：Excel格式错误!";
                return wResult;
            }

            ExcelSheetData wExcelSheetData = wExcelData.sheetData.get(0);
            CAPoHead wCAPoHead = CAPoDAO.getInstance().GetCAPoHeadByExcelSheetData(wLoginUser, wExcelSheetData,
                    wErrorCode);
            if (StringUtils.isEmpty(wCAPoHead.TableName) || wCAPoHead.PoPropList == null || wCAPoHead.PoPropList.size() <= 0) {
                wResult.FaultCode += "导入失败：Excel格式错误!";
                return wResult;
            }

            //转换为可写入的模板String
            String wPoString = CAPoDAO.getInstance().GetPoStringByCAPoHead(wLoginUser, wExcelSheetData, wCAPoHead,
                    wErrorCode);
            //创建文件
            String wFileName = StringUtils.Format("{0}.java", wExcelSheetData.sheetName);
            String wDirePath = StringUtils.Format("{0}static/export/",
                    Constants.getConfigPath().replace("config/", ""));
            String wFilePath = StringUtils.Format("{0}{1}", wDirePath, wFileName);
            File wDirFile = new File(wDirePath);
            if (!wDirFile.exists()) {
                wDirFile.mkdirs();
            }
            File wNewFile = new File(wFilePath);
            wNewFile.createNewFile();
            //写入文本
//            try (FileWriter writer = new FileWriter(wFilePath)) {
//                // 将文本写入文件，如果文件已存在则覆盖原有内容
//                writer.write(wPoString);
//            } catch (IOException e) {
//                log.error("An error occurred while writing to the file: ", e);
//            }

            try (OutputStream out = Files.newOutputStream(Paths.get(wFilePath));
                 OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
                // 将文本写入文件，如果文件已存在则覆盖原有内容
                writer.write(wPoString);
            } catch (IOException e) {
                log.error("An error occurred while writing to the file: ", e);
            }

            //输出给前端
            wResult.Result = StringUtils.Format("/{0}/export/{1}",
                    Configuration.readConfigString("project.name", "application"), wFileName);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error("BFC_ImortPo", e);
        }
        return wResult;
    }

    //endregion

    //region 消息推送
    /**
     * 待推送的消息列表
     */
    public static LinkedList<BFCMessage> ToSendList = new LinkedList<>();

    @Override
    public ServiceResult<Integer> BFC_WebSocketMessageSend(BMSEmployee wLoginUser, List<BFCMessage> wBFCMessageList) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            if (wBFCMessageList == null || wBFCMessageList.size() == 0) {
                return wResult;
            }
            wBFCMessageList.removeIf(p -> p.ID <= 0);
            if (wBFCMessageList.size() == 0) {
                return wResult;
            }

            for (BFCMessage wBFCMessage : wBFCMessageList) {
                ToSendList.offerFirst(wBFCMessage);
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error("BFC_WebSocketMessageSend", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_AutoWSSendMessage(BMSEmployee wLoginUser) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            List<Long> wSendedIDList = new ArrayList<>();
            boolean wSendResult;
            BFCMessage wBFCMessage;

            for (int i = 0; i < ToSendList.size(); i++) {
                wBFCMessage = ToSendList.poll();
                if (wBFCMessage == null) {
                    continue;
                }
                wSendResult = WebSocketServer.sendInfo(JSON.toJSONString(wBFCMessage),
                        String.valueOf(wBFCMessage.ResponsorID));
                if (!wSendResult) {
                    ToSendList.offerLast(wBFCMessage);
                }
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error("BFC_AutoWSSendMessage", e);
        }
        return wResult;
    }

    @Override
    public ServiceResult<Integer> BFC_SendMessageByID(BMSEmployee wLoginUser, int wMessaegID) {
        ServiceResult<Integer> wResult = new ServiceResult<>(0);
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            BFCMessage wBFCMessage = BFCMessageDAO.getInstance().BFC_GetMessageByID(wLoginUser, wMessaegID, wErrorCode);
            if (wBFCMessage.ID <= 0) {
                wResult.FaultCode += "推送失败：消息不存在";
                return wResult;
            }

            boolean wSendResult = WebSocketServer.sendInfo(JSON.toJSONString(wBFCMessage),
                    String.valueOf(wBFCMessage.ResponsorID));
            if (!wSendResult) {
                ToSendList.offerLast(wBFCMessage);
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error("BFC_SendMessageByID", e);
        }
        return wResult;
    }

    //endregion

//    @Override
//    public ServiceResult<Integer> BFC_CopyMenuItem(BMSEmployee wLoginUser, int wMenuItemID, int wTargetGrad,
//                                                   int wTargetGroupID) {
//        ServiceResult<Integer> wResult = new ServiceResult<>(0);
//        try {
//            OutResult<Integer> wErrorCode = new OutResult<>(0);
//
//            Map<String, Object> wParams = new HashMap<String, Object>();
//            wParams.put("ID", wMenuItemID);
//            BFCHomePageModule bfcHomePageModule = mBFCHomePageModuleService.SelectPO(wLoginUser, wParams,
//                    wErrorCode);
//            if (bfcHomePageModule != null && bfcHomePageModule.ID > 0) {
//                bfcHomePageModule.ID = 0;
//                bfcHomePageModule.Grad = wTargetGrad;
//                bfcHomePageModule.GroupID = wTargetGroupID;
//                bfcHomePageModule.setUserInfo(wLoginUser);
//                mBFCHomePageModuleService.UpdatePo(wLoginUser, bfcHomePageModule, wErrorCode);
//            }
//
//            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
//        } catch (Exception e) {
//            wResult.FaultCode += e.toString();
//            log.error("BFC_CopyMenuItem", e);
//        }
//        return wResult;
//    }
}
